package com.thingsgrid.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.thingsgrid.system.entity.Role;
import com.thingsgrid.system.entity.RoleMenu;
import com.thingsgrid.system.entity.RoleScope;
import com.thingsgrid.system.mapper.RoleMapper;
import com.thingsgrid.system.service.IRoleMenuService;
import com.thingsgrid.system.vo.RoleVO;
import lombok.AllArgsConstructor;
import org.springblade.core.secure.utils.SecureUtil;
import org.springblade.core.tool.constant.RoleConstant;
import org.springblade.core.tool.node.ForestNodeMerger;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.Func;
import com.thingsgrid.system.service.IRoleScopeService;
import com.thingsgrid.system.service.IRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.List;

/**
 * 服务实现类
 *
 * @author Chill
 */
@Service
@Validated
@AllArgsConstructor
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

	private IRoleMenuService roleMenuService;
	private IRoleScopeService roleScopeService;

	private static Integer DATA_SCOPE_CATEGORY = 1;
	private static Integer API_SCOPE_CATEGORY = 2;

	@Override
	public IPage<RoleVO> selectRolePage(IPage<RoleVO> page, RoleVO role) {
		return page.setRecords(baseMapper.selectRolePage(page, role));
	}

	@Override
	public List<RoleVO> tree(String tenantId) {
		String userRole = SecureUtil.getUserRole();
		String excludeRole = null;
		if (!CollectionUtil.contains(Func.toStrArray(userRole), RoleConstant.ADMIN) && !CollectionUtil.contains(Func.toStrArray(userRole), RoleConstant.ADMINISTRATOR)) {
			excludeRole = RoleConstant.ADMINISTRATOR;
		}
		return ForestNodeMerger.merge(baseMapper.tree(tenantId, excludeRole));
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean grant(@NotEmpty List<Long> roleIds, @NotEmpty List<Long> menuIds, List<Long> dataScopeIds, List<Long> apiScopeIds) {
		// 菜单权限模块
		// 删除角色配置的菜单集合
		roleMenuService.remove(Wrappers.<RoleMenu>update().lambda().in(RoleMenu::getRoleId, roleIds));
		// 组装配置
		List<RoleMenu> roleMenus = new ArrayList<>();
		roleIds.forEach(roleId -> menuIds.forEach(menuId -> {
			RoleMenu roleMenu = new RoleMenu();
			roleMenu.setRoleId(roleId);
			roleMenu.setMenuId(menuId);
			roleMenus.add(roleMenu);
		}));
		// 新增配置
		roleMenuService.saveBatch(roleMenus);

		// 数据权限模块
		if (CollectionUtil.isNotEmpty(dataScopeIds)) {
			// 删除角色配置的数据权限集合
			roleScopeService.remove(Wrappers.<RoleScope>update().lambda().in(RoleScope::getRoleId, roleIds));
			// 组装配置
			List<RoleScope> roleScopes = new ArrayList<>();
			roleIds.forEach(roleId -> dataScopeIds.forEach(scopeId -> {
				RoleScope roleScope = new RoleScope();
				roleScope.setScopeCategory(DATA_SCOPE_CATEGORY);
				roleScope.setRoleId(roleId);
				roleScope.setScopeId(scopeId);
				roleScopes.add(roleScope);
			}));
			// 新增配置
			roleScopeService.saveBatch(roleScopes);
		}

		// 接口权限模块
		if (CollectionUtil.isNotEmpty(apiScopeIds)) {
			// 删除角色配置的数据权限集合
			roleScopeService.remove(Wrappers.<RoleScope>update().lambda().eq(RoleScope::getScopeCategory, API_SCOPE_CATEGORY).in(RoleScope::getRoleId, roleIds));
			// 组装配置
			List<RoleScope> roleScopes = new ArrayList<>();
			roleIds.forEach(roleId -> apiScopeIds.forEach(scopeId -> {
				RoleScope roleScope = new RoleScope();
				roleScope.setScopeCategory(API_SCOPE_CATEGORY);
				roleScope.setScopeId(scopeId);
				roleScope.setRoleId(roleId);
				roleScopes.add(roleScope);
			}));
			// 新增配置
			roleScopeService.saveBatch(roleScopes);
		}

		return true;
	}

	@Override
	public List<String> getRoleNames(String roleIds) {
		return baseMapper.getRoleNames(Func.toStrArray(roleIds));
	}

	@Override
	public List<String> getRoleAliases(String roleIds) {
		return baseMapper.getRoleAliases(Func.toStrArray(roleIds));
	}

}
